;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Istockretrieval.lisp")
(require "M-avl-string-keys.lisp")

(module Mstockretrieval
  (import Itree)
  (include-book "list-utilities" :dir :teachpacks)
  (include-book "io-utilities" :dir :teachpacks)
 
; gets all the stocks within the given range of
; dates that match the given ticker name
(defun get-stocks-in-range-by-ticker (tree start end ticker)
  (if (string> start end)
      nil
      (let* ((subtree (cdr (avl-retrieve tree start)))
             (data (avl-retrieve subtree ticker)))
        (if (equal nil data)
            (get-stocks-in-range-by-ticker tree (int->str (+ 1 (str->int start))) end ticker)
            (cons data (get-stocks-in-range-by-ticker tree (int->str (+ 1 (str->int start))) end ticker))))))

; gets all the stocks in the given range that
; match one of the names in tickers using
; get-stocks-in-range-by-ticker as a helper
(defun get-stocks-in-range-by-tickers (tree start end tickers)
  (if (consp tickers)
      (concatenate 'list (get-stocks-in-range-by-ticker tree start end (car tickers))
                         (get-stocks-in-range-by-tickers tree start end (cdr tickers)))
      nil))

; gets all the stocks within the given range of
; dates from the given tree
(defun get-stocks-in-range (tree start end)
  (if (string> start end)
      nil
      (if (equal nil (avl-retrieve tree start))
          (get-stocks-in-range tree (int->str (+ 1 (str->int start))) end)
          (concatenate 'list (avl-flatten (cdr (avl-retrieve tree start))) 
                       (get-stocks-in-range tree (int->str (+ 1 (str->int start))) end)))))

; gets the first occurrence of the given ticker
; in the range of dates since not all tickers
; occur on all dates
(defun get-first-in-range (tree start end ticker)
  (let* ((stock (avl-retrieve (cdr (avl-retrieve tree start)) ticker)))
    (if (equal nil stock)
        (if (> (str->int start) (str->int end))
            nil
            (get-first-in-range tree (int->str (+ 1 (str->int start))) end ticker))
        stock)))

; gets the last occurrence of the given ticker
; in the range of dates since not all tickers
; occur on all dates
(defun get-last-in-range (tree start end ticker)
  (let* ((stock (avl-retrieve (cdr (avl-retrieve tree end)) ticker)))
    (if (equal nil stock)
        (if (> (str->int start) (str->int end))
            nil
            (get-last-in-range tree start (int->str (- (str->int end) 1)) ticker))
        stock)))

  
(defun get-all-tickers-helper (stocks)
  (if (consp stocks)
      (cons (caar stocks) (get-all-tickers-helper (cdr stocks)))
      nil))
  
; takes the results of get-stocks-in-range and returns
; all of the tickers that occur in that range.
(defun get-all-tickers (stocks)
  (remove-duplicates-equal (get-all-tickers-helper stocks)))
  
  (export Istockretrieval))